package dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * Course entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see dao.Course
 * @author MyEclipse Persistence Tools
 */

public class CourseDAO extends BaseHibernateDAO {
    private static final Logger log = LoggerFactory.getLogger(CourseDAO.class);
    // property constants
    public static final String NAME = "name";
    public static final String DESCRIPTION = "description";

    public boolean save(Course transientInstance) {
	return save(log, "saving Course instance", transientInstance);
    }

    public boolean delete(Course persistentInstance) {
	return delete(log, "deleting Course instance", persistentInstance);
    }

    public int getCourseCounts(){
	try {
	    String queryString = "select count(courseid) from Course";
	    Query queryObject = getSession().createQuery(queryString);
	    return (Integer)queryObject.uniqueResult();
	} catch (RuntimeException re) {
	    throw re;
	}
    }
    public Course findById(java.lang.String id) {
	log.debug("getting Course instance with id: " + id);
	try {
	    Course instance = (Course) getSession().get("dao.Course", id);
	    return instance;
	} catch (RuntimeException re) {
	    log.error("get failed", re);
	    throw re;
	}
    }

    public List<Course> findByExample(Course instance) {
	log.debug("finding Course instance by example");
	try {
	    List<Course> results = (List<Course>) getSession()
		    .createCriteria("dao.Course").add(create(instance)).list();
	    log.debug("find by example successful, result size: "
		    + results.size());
	    return results;
	} catch (RuntimeException re) {
	    log.error("find by example failed", re);
	    throw re;
	}
    }

    public List findByProperty(String propertyName, Object value) {
	log.debug("finding Course instance with property: " + propertyName
		+ ", value: " + value);
	try {
	    String queryString = "from Course as model where model."
		    + propertyName + "= ?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, value);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find by property name failed", re);
	    throw re;
	}
    }

    public List<Course> findByName(Object name) {
	return findByProperty(NAME, name);
    }

    public List<Course> findByDescription(Object description) {
	return findByProperty(DESCRIPTION, description);
    }

    public List<Course> findAll() {
	log.debug("finding all Course instances");
	try {
	    String queryString = "from Course";
	    Query queryObject = getSession().createQuery(queryString);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	}
    }
    public List<Course> findByGrade(String grade){
	log.debug("finding Course instances by teacherid");
	try {
	    String queryString = "from Course where courseid like ?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setString(0, grade+"__");
	    List<Course> list = queryObject.list();
	    if(list.size() > 0)
		return list;
	    else{
		return null;
	    }
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	}
    }
    
    public Course findByTeacherid(String idString){
	log.debug("finding Course instances by teacherid");
	try {
	    String queryString = "select mod1 from Course mod1,Teacher mod2 where mod1.courseid=mod2.courseid and mod2.teacherid=?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setString(0, idString);
	    List<Course> list = queryObject.list();
	    if(list.size() > 0)
		return (Course)list.get(0);
	    else{
		return null;
	    }
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	}
    }

    public Course merge(Course detachedInstance) {
	log.debug("merging Course instance");
	try {
	    Course result = (Course) getSession().merge(detachedInstance);
	    log.debug("merge successful");
	    return result;
	} catch (RuntimeException re) {
	    log.error("merge failed", re);
	    throw re;
	}
    }

    public void attachDirty(Course instance) {
	attachDirty(log, "attaching dirty Course instance", instance);
    }

    public boolean update(Course instance) {
	return update(log, "updating Course instance", instance);
    }
    
    public void attachClean(Course instance) {
	log.debug("attaching clean Course instance");
	try {
	    getSession().lock(instance, LockMode.NONE);
	    log.debug("attach successful");
	} catch (RuntimeException re) {
	    log.error("attach failed", re);
	    throw re;
	}
    }
    
    public static void main(String[] args){
	CourseDAO courseDAO = new CourseDAO();
	System.out.println(courseDAO.findByGrade("01").get(0).getName());
    }
}